<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/8/15
 * Time: 10:00
 */

namespace common\components\widgets;


use dmstr\widgets\Menu;
use yii\helpers\ArrayHelper;
use yii\helpers\Url;

class SidebarMenu extends Menu
{
    /**
     * @inheritdoc
     */
    public $linkTemplate = '<a data-url="{data-url}" href="{url}"  class="{class}">{icon} {label}</a>';
    public $submenuTemplate = "\n<ul class='treeview-menu' {show}>\n{items}\n</ul>\n";
    public $activateParents = true;
    /**
     * @inheritdoc
     */
    protected function renderItem($item)
    {
        $str = '';
        if(isset($item['items'])) {
            $labelTemplate = '<a data-url="{data-url}" href="{url}" >{label} <i class="fa fa-angle-left pull-right"></i></a>';
            $linkTemplate = '<a data-url="{data-url}" href="{url}"  >{icon} {label} <i class="fa fa-angle-left pull-right"></i></a>';
        }
        else {
            $labelTemplate = $this->labelTemplate;
            $linkTemplate = $this->linkTemplate;
        }
        //die($str.'--'. \Yii::$app->controller->id);

        if (isset($item['url'])) {
            $template = ArrayHelper::getValue($item, 'template', $linkTemplate);
            $replace = !empty($item['icon']) ? [
                '{data-url}' => $item['data-url'],
                '{url}' => Url::to($item['url']),
                '{label}' => '<span>'.$item['label'].'</span>',
                '{icon}' => '<i class="' . $item['icon'] . '"></i> ',
                '{class}'=> $item['class'],
            ] : [
                '{data-url}' => $item['data-url'],
                '{url}' => Url::to($item['url']),
                '{label}' => '<span>'.$item['label'].'</span>',
                '{icon}' => null,
                '{class}'=> $item['class'],
            ];
            return strtr($template, $replace);
        } else {
            $template = ArrayHelper::getValue($item, 'template', $labelTemplate);
            $replace = !empty($item['icon']) ? [
                '{label}' => '<span>'.$item['label'].'</span>',
                '{icon}' => '<i class="' . $item['icon'] . '"></i> '
            ] : [
                '{label}' => '<span>'.$item['label'].'</span>',
            ];
            return strtr($template, $replace);
        }
    }
}